﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class UserInDepartments_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(UserInDepartment entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.UserInDepartmentSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string userId, string departmentId)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid userid = Guid.Parse(userId);
                Guid deptid = Guid.Parse(departmentId);
                UserInDepartment entity = context.UserInDepartmentSet.FirstOrDefault(c => c.UserId == userid && c.DepartmentId == deptid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public UserInDepartment Query(string userId, string departmentId)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid userid = Guid.Parse(userId);
                Guid deptid = Guid.Parse(departmentId);
                UserInDepartment entity = context.UserInDepartmentSet.FirstOrDefault(c => c.UserId == userid && c.DepartmentId == deptid);
                return entity;
            }
        }
        /// <summary>
        /// 根据部门查询所有用户
        /// </summary>
        /// <param name="departmentId">部门Id</param>
        /// <returns>查询结果实体</returns>
        public List<UserInDepartment> Query(string departmentId)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid deptid = Guid.Parse(departmentId);

                var q = from c in context.UserInDepartmentSet
                        where c.DepartmentId == deptid
                        select c;
                List<UserInDepartment> list = new List<UserInDepartment>();
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<UserInDepartment> QueryAll()
        {
            List<UserInDepartment> list = new List<UserInDepartment>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.UserInDepartmentSet
                        select c;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }
    }
}
